import time

from selenium import webdriver
from selenium.webdriver.common.by import By

# 实例化Options来创建启动参数对象
options = webdriver.ChromeOptions()
# 启动无痕模式运行
options.add_argument('--incognito')
# 设置窗口最大化运行
options.add_argument('--start-maximized')
# 取消Chrome正受到自动测试软件的控制的提示
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option("excludeSwitches", ['enable-automation'])
# 启动浏览器
driver = webdriver.Chrome(options=options)
# 设置隐式等待
driver.implicitly_wait(3)
# 防止被反爬
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    "source": """
    Object.defineProperty(navigator, 'webdriver', {
      get: () => false
    })
  """
})

# 访问中国铁路12306网站
driver.get('https://www.12306.cn/')
time.sleep(3)

# 出发地：西安(代号：XAY)
fromStation = driver.find_element(By.ID, 'fromStation')
driver.execute_script("arguments[0].value='XAY';", fromStation)
time.sleep(1)

# 到达地：信阳(代号：XUN)
toStation = driver.find_element(By.ID, 'toStation')
driver.execute_script("arguments[0].value='XUN';", toStation)
time.sleep(1)

# 出发日期
train_date = driver.find_element(By.ID, 'train_date')
driver.execute_script(f"arguments[0].value='{time.strftime('%Y-%m-%d')}';", train_date)
time.sleep(1)

# 点击查询
driver.find_element(By.ID, 'search_one').click()
time.sleep(3)

driver.quit()
